#include "ADUIntroScene.h"

IntroLayer::IntroLayer() {
    // Load plist files
    SpriteFrameCache* cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("Intro/mother_duck.plist", "Intro/mother_duck.png");
    cache->addSpriteFramesWithFile("Intro/duck_kid.plist", "Intro/duck_kid.png");
    cache->addSpriteFramesWithFile("Intro/ugly_duck.plist", "Intro/ugly_duck.png");
    cache->addSpriteFramesWithFile("Intro/ugly_fall.plist", "Intro/ugly_fall.png");
}
IntroLayer::~IntroLayer() {

}
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
cocos2d::Scene* IntroLayer::createScene() {
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = IntroLayer::create();
    // add layer as a child to scene
    scene->addChild(layer,1);

    // return the scene
    return scene;
}

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
bool IntroLayer::init()  {
	mSpeed = 35.0f;
    mFallSpeed = 60.0f;
    mChangeAnimation = false;
    mFirst = false;
    mTimer = 4.0f;
    currentBG = -1;
	Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    float centerX = origin.x + visibleSize.width/2;
    float centerY = origin.y + visibleSize.height/2;
    mThresholdX = visibleSize.width * 0.18;
    mThresholdY = visibleSize.height * 0.25;
    // Add background 
    mBackground = Sprite::create(IMG_INTRO_BG_1);
    mBackground->setPosition(centerX, centerY);
    this->addChild(mBackground);

    // Add duck_mother
	mDuckMother = Sprite::createWithSpriteFrameName("mother_walk_001.png");
	mSizeDuckMother = mDuckMother->getContentSize();
    mDuckMother->setPosition(visibleSize.width * 0.75, visibleSize.height * 0.25);
    this->addChild(mDuckMother);
    doDuckMotherWalk();

    Vec2 motherPos = mDuckMother->getPosition();
    float delta = visibleSize.width/18;
    // Add child duck
    for(int i = 0; i < 4; i++) {
    	mDuckKid[i] = Sprite::createWithSpriteFrameName("kid_walk_001.png");
    	if(i%2 == 0) {
	    	mDuckKid[i]->setPosition(motherPos.x - (delta * (i+1)), motherPos.y);	
    	} else {
    		mDuckKid[i]->setPosition(motherPos.x - (delta * (i+1)), motherPos.y - visibleSize.height/15 );
    	}
    	
    	this->addChild(mDuckKid[i]);
    }
    doDuckChildWalk();

    

    // Add ugly duckling
    mUglyDuck = Sprite::createWithSpriteFrameName("Ugly_walk_001.png");
    mUglyDuck->setPosition(0, visibleSize.height * 0.18);
    this->addChild(mUglyDuck);
    doUglyDuckWalk();
    
    // Update
    this->scheduleUpdate();
    return true;
}
      
void IntroLayer::update(float dt) {

    if(mChangeAnimation == false) 
    	duckMove(dt);
    else {
        if(mFirst) {
            mBackground->setTexture(IMG_INTRO_BG_2);
            stopAction();
            // Set position of ugly duck
            Vec2 uglyPos = mUglyDuck->getPosition();
            Size visibleSize = Director::getInstance()->getVisibleSize();
            uglyPos.y = visibleSize.height;
            mUglyDuck->setPosition(uglyPos);
            doUglyDuckFall();
            mFirst = false;
        }
        duckFall(dt); 
    }
       
}

void IntroLayer::stopAction() {
    mDuckMother->stopAllActions();
    mDuckMother->setVisible(false);
    mUglyDuck->stopAllActions();
    for(int i=0; i<4; i++) {
        mDuckKid[i]->stopAllActions();
        mDuckKid[i]->setVisible(false);
    }
}

void IntroLayer::doDuckMotherWalk() {
	Vector<SpriteFrame*> animFrames(2);
    char str[100] = {0};
    for(int i=0; i < 8; i++) {
        sprintf(str, "mother_walk_00%d.png", i+1);
        SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str);
        animFrames.pushBack(frame);
    }
    Animation* runAnimation = Animation::createWithSpriteFrames(animFrames, ANIMATION_INTERVAL);
    if(mDuckMother) {
	    mDuckMother->runAction(RepeatForever::create(Animate::create(runAnimation)));	
    }
}

void IntroLayer::doDuckChildWalk() {
	Vector<SpriteFrame*> animFrames(2);
    char str[100] = {0};
    for(int i=0; i < 4; i++) {
        sprintf(str, "kid_walk_00%d.png", i+1);
        SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str);
        animFrames.pushBack(frame);
    }
    Animation* runAnimation = Animation::createWithSpriteFrames(animFrames, ANIMATION_INTERVAL);
    for(int i = 0; i < 4; i++) {
    	if(mDuckKid[i]) {
	    	mDuckKid[i]->runAction(RepeatForever::create(Animate::create(runAnimation)));		
    	}
    }
}

void IntroLayer::doUglyDuckWalk() {
	Vector<SpriteFrame*> animFrames(2);
    char str[100] = {0};
    for(int i=0; i < 8; i++) {
        sprintf(str, "Ugly_walk_00%d.png", i+1);
        SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str);
        animFrames.pushBack(frame);
    }
    Animation* runAnimation = Animation::createWithSpriteFrames(animFrames, ANIMATION_INTERVAL);    
	if(mUglyDuck) {
    	mUglyDuck->runAction(RepeatForever::create(Animate::create(runAnimation)));		
	}
}

void IntroLayer::doUglyDuckFall() {
    Vector<SpriteFrame*> animFrames(6);
    char str[100] = {0};
    for(int i=0; i < 6; i++) {
        sprintf(str, "Ugly_walk_fall_00%d.png", i+1);
        SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str);
        animFrames.pushBack(frame);
    }
    Animation* runAnimation = Animation::createWithSpriteFrames(animFrames, 1.0f);    
    if(mUglyDuck) {
        mUglyDuck->runAction(RepeatForever::create(Animate::create(runAnimation)));     
    }
}


void IntroLayer::duckMove(float dt) {
	float s = mSpeed * dt;
	Vec2 duckMotherPos = mDuckMother->getPosition();
	Vec2 uglyPosition = mUglyDuck->getPosition();

    if(uglyPosition.x >= mThresholdX) {
        log("Change Animation");
        mChangeAnimation = true;
        mFirst = true;
        return;
    }

	mDuckMother->setPosition(duckMotherPos.x + s, duckMotherPos.y);
	mUglyDuck->setPosition(uglyPosition.x + s, uglyPosition.y);

	for(int i = 0; i < 4; i++) {
		Vec2 pos = mDuckKid[i]->getPosition();
		mDuckKid[i]->setPosition(pos.x + s, pos.y);
	}
}

void IntroLayer::duckFall(float dt) {
    float s = mFallSpeed * dt;
    Vec2 uglyPosition = mUglyDuck->getPosition();
    if(uglyPosition.y <= mThresholdY) {
        mUglyDuck->stopAllActions();
        mUglyDuck->setVisible(false);
        mTimer -= dt;
        if(mTimer <= 0) {
            GameManager::getInstance()->RunScene(GAMEPLAY_SCENE);
        }else if (mTimer <= 1.0 && currentBG < 3){
        	mBackground->setTexture(IMG_SHOT_3);
        	currentBG = 3;
        }else if (mTimer <= 2.0 && currentBG < 2){
        	mBackground->setTexture(IMG_SHOT_2);
        	currentBG = 2;
        }else if (mTimer <= 3.0 && currentBG < 1){
        	mBackground->setTexture(IMG_SHOT_1);
        	currentBG = 1;
        }else if (mTimer <= 4.0 && currentBG < 0){
        	mBackground->setTexture(IMG_INTRO_BG_3);
        	currentBG = 0;
        }
    }
    mUglyDuck->setPosition(uglyPosition.x , uglyPosition.y - s);
}
